global root "C:\Users\chupilkm\EBRD\OCE - Maxim\War data\Earthquakes replication"


**Cleaning observations such as location "China;India"

clear
use "$root\Data\Raw data\earthquakes data.dta" 
drop if mag == .
drop if deaths == .
drop if mag < 6
drop if deaths < 100
replace country = "USA" if country == "CALIFORNIA"
kountry country, from (other) stuck
*drop NAMES_STD
rename _ISO3N_ ison
kountry ison, from(iso3n) to(iso3c)
rename _ISO3C_ iso_code
replace iso_code = "TWN" if country == "TAIWAN"
drop if iso_code == ""
gen earthquake = 1
keep iso_code earthquake year deaths mag
collapse (sum) deaths (mean) earthquake mag, by( iso_code year)
save "$root\Data\Working data\earthquakes.dta", replace

****Generate macroeconomic dataset to merge with earthquakes data (IMF+Maddison tables)
clear
use "$root\Data\Raw data\Maddison_GDP.dta" 
rename countrycode iso_code
replace gdppc = . if gdppc == 0
replace iso_code = "WBG" if iso_code == "PSE"
drop if iso_code == "CZE" & year < 1993
replace iso_code = "CZE" if iso_code == "CSK" & year < 1993
drop if iso_code == "CSK"
save "$root\Data\Working data\Maddison2.dta", replace

clear
use "$root\Data\Raw data\WEO_full.dta" if  WEOSubjectCode == "NID_NGDP" | WEOSubjectCode == "NGSD_NGDP" | WEOSubjectCode == "PCPIPCH" |WEOSubjectCode == "TX_RPCH" |WEOSubjectCode == "TM_RPCH" |WEOSubjectCode == "LUR" |WEOSubjectCode == "GGR_NGDP" |WEOSubjectCode == "GGX_NGDP" |WEOSubjectCode == "GGXCNL_NGDP" |WEOSubjectCode == "GGXWDG_NGDP" |WEOSubjectCode == "BCA_NGDPD"|WEOSubjectCode == "NGDPRPPPPC"|WEOSubjectCode == "GGXONLB_NGDP"|WEOSubjectCode == "NGDP_R"|WEOSubjectCode == "LP"
reshape long y, i( ISO WEOSubjectCode ) j(year)
drop SubjectNotes
drop Scale CountrySeriesspecificNotes
drop WEOCountryCode Country SubjectDescriptor Units
reshape wide y, i(ISO year ) j( WEOSubjectCode ) string
rename yBCA_NGDPD current_account_GDP
rename yGGR_NGDP government_revenue_GDP
rename yGGXCNL_NGDP government_balance_GDP
rename yGGXWDG_NGDP government_gross_debt_GDP
rename yGGX_NGDP government_total_expense_GDP
rename yLUR unemployment
rename yNGSD_NGDP savings_GDP
rename yNID_NGDP investment_GDP
rename yPCPIPCH inflation
rename yTM_RPCH import_growth
rename yTX_RPCH export_growth
rename yNGDPRPPPPC real_GDP_pc_IMF
rename yGGXONLB_NGDP primary_balance_GDP
rename yLP population
rename yNGDP_R real_GDP
rename ISO iso_code
replace iso_code ="XKX" if iso_code == "UVK"
destring current_account_GDP government_revenue_GDP government_balance_GDP government_gross_debt_GDP government_total_expense_GDP unemployment real_GDP_pc_IMF savings_GDP investment_GDP inflation import_growth export_growth primary_balance_GDP real_GDP population, replace force
save "$root\Data\Working data\IMF_WEO2.dta", replace

clear
use "$root\Data\Working data\Maddison2.dta"
merge 1:1 iso_code year using "$root\Data\Working data\IMF_WEO2.dta"
drop _merge
sort iso_code year
save "$root\Data\Working data\economic_data_earthquake.dta", replace


***merging earthquake and war data


clear
use "$root\Data\Working data\economic_data_earthquake.dta"
merge 1:1 iso_code year using "$root\Data\Working data\earthquakes.dta"
drop _merge
sort iso_code year
order iso_code year


*dropping all the Soviet Union and Yugoslavia observations

foreach code in "RUS" "BLR" "ARM" "AZE" "GEO" "KAZ" "KGZ" "RUS" "TJK" "TKM" "UKR" "UZB" {
	drop if iso_code == "`code'" & year >= 1922 & year < 1986
}

foreach code in  "EST" "LVA" "LTU" "MDA"  {
	drop if iso_code == "`code'" & year >= 1922 & year < 1986
}

foreach code in  "BIH" "HRV" "MKD" "MNE" "SRB" "SVN" {
	drop if iso_code == "`code'" & year >= 1922 & year < 1987
}

drop if iso_code == "SUN" & year < 1915 
drop if iso_code == "SUN" & year > 1991
drop if iso_code == "YUG" & year < 1900 
drop if iso_code == "YUG" & year > 1992
drop if year > 2022

replace earthquake = 0 if earthquake == .
gen deaths_share = (deaths*100)/(pop*1000)
replace earthquake = 0 if deaths_share < 0.001

sort iso_code year
by iso_code: gen year_pre_earthquake_1 = 1 if earthquake[_n+1] == 1 & earthquake[_n] == 0
by iso_code: gen year_pre_earthquake_2 = 1 if earthquake[_n+2] == 1 & earthquake[_n] == 0 & earthquake[_n+1] == 0
by iso_code: gen year_pre_earthquake_3 = 1 if earthquake[_n+3] == 1 & earthquake[_n] == 0 & earthquake[_n+1] == 0& earthquake[_n+2] == 0
by iso_code: gen year_pre_earthquake_4 = 1 if earthquake[_n+4] == 1 & earthquake[_n] == 0 & earthquake[_n+1] == 0& earthquake[_n+2] == 0& earthquake[_n+3] == 0
by iso_code: gen year_pre_earthquake_5 = 1 if earthquake[_n+5] == 1 & earthquake[_n] == 0 & earthquake[_n+1] == 0& earthquake[_n+2] == 0& earthquake[_n+3] == 0& earthquake[_n+4] == 0
by iso_code: gen year_post_earthquake_1 = 1 if earthquake[_n-1] == 1 & earthquake[_n] == 0
by iso_code: gen year_post_earthquake_2 = 1 if earthquake[_n-2] == 1 & earthquake[_n] == 0& earthquake[_n-1] == 0
by iso_code: gen year_post_earthquake_3 = 1 if earthquake[_n-3] == 1 & earthquake[_n] == 0& earthquake[_n-1] == 0& earthquake[_n-2] == 0
by iso_code: gen year_post_earthquake_4 = 1 if earthquake[_n-4] == 1 & earthquake[_n] == 0& earthquake[_n-1] == 0& earthquake[_n-2] == 0& earthquake[_n-3] == 0
by iso_code: gen year_post_earthquake_5 = 1 if earthquake[_n-5] == 1 & earthquake[_n] == 0& earthquake[_n-1] == 0& earthquake[_n-2] == 0& earthquake[_n-3] == 0& earthquake[_n-4] == 0

replace year_pre_earthquake_1 = 0  if year_pre_earthquake_1 == .
replace year_pre_earthquake_2 = 0  if year_pre_earthquake_2 == .
replace year_pre_earthquake_3 = 0  if year_pre_earthquake_3 == .
replace year_pre_earthquake_4 = 0  if year_pre_earthquake_4 == .
replace year_pre_earthquake_4 = 0  if year_pre_earthquake_4 == .
replace year_pre_earthquake_5 = 0  if year_pre_earthquake_5 == .
replace year_post_earthquake_1 = 0  if year_post_earthquake_1 == .
replace year_post_earthquake_2 = 0  if year_post_earthquake_2 == .
replace year_post_earthquake_3 = 0  if year_post_earthquake_3 == .
replace year_post_earthquake_4 = 0  if year_post_earthquake_4 == .
replace year_post_earthquake_5 = 0  if year_post_earthquake_5 == .

sort iso_code year
by iso_code: gen GDP_pc_growth_Madisson = gdppc/ gdppc[_n-1]*100-100 if iso_code[_n-1] == iso_code & year[_n-1] == year-1


encode iso_code, generate(iso_code2)

merge 1:1 iso_code year using  "$root\Data\Raw data\pwt100.dta"
drop if _merge ==2
drop _merge

merge 1:1 iso_code year using  "$root\Data\Raw data\population density.dta"
drop if _merge ==2
drop _merge

merge 1:1 iso_code year using "$root\Data\Raw data\debt_to_GDP_data.dta"
drop if _merge == 2
drop _merge

save "$root\Data\Working data\full_earthquakes.dta", replace